package guoanmaker.personal.business.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.personal.business.model.OperatorSensitiveWord;
import guoanmaker.personal.business.repository.OperatorSensitiveWordRepository;
import guoanmaker.personal.helper.common.util.SensitiveWordInit;
import guoanmaker.personal.helper.common.util.SensitivewordEngine;

@Service
public class OperatorSensitiveWordService{

	@Autowired
	private OperatorSensitiveWordRepository operatorSensitiveWordRepository;
	public List<OperatorSensitiveWord> getAll(){
		return operatorSensitiveWordRepository.findAll();
	}
	public Set<String> sensitiveWordVerification(String text){
		text=text==null||text.replace(" ","").equals("")?"":text.replace(" ","");
		System.out.println("共有："+text.length()+"字符");
		Long t1=System.currentTimeMillis();
		SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
        // 从数据库中获取敏感词对象集合（调用的方法来自Dao层，此方法是service层的实现类）
        List<OperatorSensitiveWord> sensitiveWords = getAll();
        // 构建敏感词库
        Map sensitiveWordMap = sensitiveWordInit.initKeyWord(sensitiveWords);
        // 传入SensitivewordEngine类中的敏感词库
        Long t2=System.currentTimeMillis();
        System.out.println("构建敏感词库所用时间："+(t2-t1)+"毫秒");
        SensitivewordEngine.sensitiveWordMap = sensitiveWordMap;
        // 得到敏感词有哪些，传入2表示获取所有敏感词
        Set<String> set = SensitivewordEngine.getSensitiveWord(text, 2);
        Long t3=System.currentTimeMillis();
        System.out.println("过滤所用时间："+(t3-t2)+"毫秒");
        return set;
	}
}
